package com.camel.base.utils;

import com.alibaba.fastjson.JSONObject;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Administrator
 */
public class StringFormatUtil {
    private static final Pattern PATTERN = Pattern.compile("\\#\\{(.*?)\\}");
    private static Matcher matcher;

    public static void main(String[] args) {
        String a = "#{地区} cat cat cattie cat";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("地区", "杭州市");

        String s = stringFormat(a, jsonObject);
        System.out.println(s);
    }

    /**
     * 格式化字符串 字符串中使用{key}表示占位符
     *
     * @param sourStr 需要匹配的字符串
     * @param param   参数集
     * @return
     */
    public static String stringFormat(String sourStr, Map<String, Object> param) {
        String tagerStr = sourStr;
        if (param == null) {
            return tagerStr;
        }
        try {
            matcher = PATTERN.matcher(tagerStr);
            while (matcher.find()) {
                String key = matcher.group();
                String keyclone = key.substring(2, key.length() - 1).trim();
                Object value = param.get(keyclone);
                if (value != null) {
                    tagerStr = tagerStr.replace(key, value.toString());
                }
            }
        } catch (Exception e) {
            return null;
        }
        return tagerStr;
    }

    /**
     * 格式化字符串 字符串中使用{key}表示占位符 利用反射 自动获取对象属性值 (必须有get方法)
     *
     * @param sourStr 需要匹配的字符串
     * @return
     */
    public static String stringFormat(String sourStr, Object obj) {
        String tagerStr = sourStr;
        matcher = PATTERN.matcher(tagerStr);
        if (obj == null) {
            return tagerStr;
        }
        PropertyDescriptor pd;
        Method getMethod;
        // 匹配{}中间的内容 包括括号
        while (matcher.find()) {
            String key = matcher.group();
            String keyclone = key.substring(2, key.length() - 1).trim();
            try {
                pd = new PropertyDescriptor(keyclone, obj.getClass());
                // 获得get方法
                getMethod = pd.getReadMethod();
                Object value = getMethod.invoke(obj);
                if (value != null) {
                    tagerStr = tagerStr.replace(key, value.toString());
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                // Loggers.addException(e);
            }
        }
        return tagerStr;
    }

    /**
     * 格式化字符串 (替换所有) 字符串中使用{key}表示占位符
     *
     * @param sourStr 需要匹配的字符串
     * @param param   参数集
     * @return
     */
    public static String stringFormatAll(String sourStr, Map<String, Object> param) {
        String tagerStr = sourStr;
        if (param == null) {
            return tagerStr;
        }
        try {
            matcher = PATTERN.matcher(tagerStr);
            while (matcher.find()) {
                String key = matcher.group();
                String keyclone = key.substring(2, key.length() - 1).trim();
                Object value = param.get(keyclone);
                if (value != null) {
                    tagerStr = tagerStr.replace(key, value.toString());
                }
            }
        } catch (Exception e) {
            return null;
        }
        return tagerStr;
    }

    /**
     * 格式花字符串，按照占位符名字
     * 输入：sourStr = xxxxx{a}xxxx{b} ,param = {a:A,b:B}
     * 输出：targetStr = xxxxAxxxxB
     *
     * @param sourStr
     * @param param
     * @return
     */
    public static String stringFormat(String sourStr, JSONObject param) {
        String tagerStr = sourStr;
        if (param == null) {
            return tagerStr;
        }
        try {
            matcher = PATTERN.matcher(tagerStr);
            while (matcher.find()) {
                String key = matcher.group();
                String keyclone = key.substring(2, key.length() - 1).trim();
                Object value = param.get(keyclone);
                if (value != null) {
                    tagerStr = tagerStr.replace(key, value.toString());
                }
            }
        } catch (Exception e) {
            return null;
        }
        return tagerStr;
    }


}
